package com.yanzhang.servlet;

import com.alibaba.fastjson.JSONArray;
import com.mysql.cj.util.StringUtils;
import com.yanzhang.pojo.Provider;
import com.yanzhang.pojo.User;
import com.yanzhang.service.provider.ProviderService;
import com.yanzhang.service.provider.ProviderServiceImpl;
import com.yanzhang.util.Constants;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * 供应商增删改查
 * @author yanzhang
 */
public class ProviderServlet extends HttpServlet {

    private ProviderService providerService;

    public ProviderServlet() {
        providerService = new ProviderServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "query": {
                query(req, resp);
                break;
            }
            case "add": {
                addProvider(req, resp);
                break;
            }
            case "view": {
                viewProvider(req, resp, "/jsp/providerview.jsp");
                break;
            }
            case "toModify": {
                viewProvider(req, resp, "/jsp/providermodify.jsp");
                break;
            }
            case "modify": {
                modifyProvider(req, resp);
                break;
            }
            case "deleteProvider": {
                deleteProvider(req, resp);
                break;
            }
            default: {

                break;
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    /**
     * 条件查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String queryProCode = req.getParameter("queryProCode");
        String queryProName = req.getParameter("queryProName");
        List<Provider> providerList = providerService.getProviderList(queryProName, queryProCode);
        req.setAttribute("queryProCode", queryProCode);
        req.setAttribute("queryProName", queryProName);
        req.setAttribute("providerList", providerList);
        req.getRequestDispatcher("/jsp/providerlist.jsp").forward(req, resp);
    }

    /**
     * 新增一个供应商
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void addProvider(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");
        Object userSession = req.getSession().getAttribute(Constants.USER_SESSION);
        User user = (User) userSession;
        Provider provider = new Provider(proCode, proName, proDesc, proContact, proPhone, proAddress, proFax, user.getId(), new Date());
        if (providerService.addProvider(provider)) {
            resp.sendRedirect(req.getContextPath() + "/provider?method=query");
        } else {
            req.getRequestDispatcher("/jsp/provideradd.jsp").forward(req, resp);
        }
    }

    /**
     * 获取供应商信息
     * @param req
     * @param resp
     * @param uri
     * @throws ServletException
     * @throws IOException
     */
    private void viewProvider(HttpServletRequest req, HttpServletResponse resp, String uri) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("proid"));
        Provider provider = providerService.getProviderById(id);
        if (provider == null) {
            resp.sendRedirect(req.getContextPath() + "/provider?method=query");
        } else {
            req.setAttribute("provider", provider);
            req.getRequestDispatcher(uri).forward(req, resp);
        }
    }

    /**
     * 修改供应商信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void modifyProvider(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("proid"));
        String proCode = req.getParameter("proCode");
        String proName = req.getParameter("proName");
        String proContact = req.getParameter("proContact");
        String proPhone = req.getParameter("proPhone");
        String proAddress = req.getParameter("proAddress");
        String proFax = req.getParameter("proFax");
        String proDesc = req.getParameter("proDesc");
        Object userSession = req.getSession().getAttribute(Constants.USER_SESSION);
        User user = (User) userSession;
        Provider provider = new Provider(id, proCode, proName, proDesc, proContact, proPhone, proAddress, proFax, null ,null, user.getId(), new Date());
        if (providerService.modifyProvider(provider)) {
            resp.sendRedirect(req.getContextPath() + "/provider?method=query");
        } else {
            req.getRequestDispatcher("/jsp/providermodify.jsp").forward(req, resp);
        }
    }

    /**
     * 删除一个供应商
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void deleteProvider(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String proid = req.getParameter("proid");
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        if (StringUtils.isNullOrEmpty(proid)) {
            writer.write(JSONArray.toJSONString("notExist"));
        } else {
            try {
                int id = Integer.parseInt(proid);
                if (providerService.deleteProviderById(id)) {
                    writer.write(JSONArray.toJSONString(true));
                } else {
                    writer.write(JSONArray.toJSONString(false));
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
                writer.write(JSONArray.toJSONString("notExist"));
            } finally {
                writer.close();
            }
        }
        writer.close();
    }
}
